import java.util.HashSet;

/**
 * @projectName: LeetCode
 * @package: PACKAGE_NAME
 * @className: Point3
 * @author: Mocrash
 * @description: TODO
 * @date: 2024/10/14 11:07
 * @version: 1.0
 */
public class Point3 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int i = solution.lengthOfLongestSubstring("ababaaa");
        System.out.println(i);
    }

    private static class Solution {
        public int lengthOfLongestSubstring(String s) {
            if (s.length() == 0){
                return 0;
            }
            char[] str = s.toCharArray();
            int i = 0, j = 0, maxLength = 1;
            HashSet<Character> hashSet = new HashSet<>();
            while (j < str.length) {
                if (hashSet.contains(str[j])) {
                    while (i < j) {

                        if (str[i] == str[j]){
                            i++;
                            break;
                        }
                        hashSet.remove(str[i]);
                        i++;
                    }
                } else {
                    hashSet.add(str[j]);
                }
                maxLength = Math.max(j - i + 1, maxLength);
                j++;
            }
            return maxLength;
        }
    }
}

